define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area", ["100%", "100%"]); //添加
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kf/pigai/index' + location.search,
                    add_url: 'kf/pigai/add',
                    edit_url: 'kf/pigai/edit',
                    del_url: 'kf/pigai/del',
                    multi_url: 'kf/pigai/multi',
                    import_url: 'kf/pigai/import',
                    table: 'list_three',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id'), operate: false},
                        {
                            field: 'batch_state',
                            title: __('类型'),
                            operate: 'LIKE',
                            searchList: {
                                "1": __('退'),
                                "2": __('改'),
                                "3": __('增'),
                            },
                            formatter: function (value, row, index) {
                                var html = '';
                                if (value == 2) {
                                    html = '<p style="color:#0090ff;font-weight: 700;border: 1px solid #0090ff;border-radius: 4px;width: 22px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;">改</p>';
                                } else if (value == 3) {
                                    html = '<p style="color:#F00;font-weight: 700;border: 1px solid #F00;border-radius: 4px;width: 22px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;">增</p>';
                                } else if (value == 1) {
                                    html = '<p style="color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 22px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;">退</p>';
                                }
                                return html;
                            }
                        },
                        {
                            field: 'pigai_numz',
                            title: __('批单暂存号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'pigai_nump',
                            title: __('批单号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'zuzhi.name',
                            title: __('组织'),
                            searchList: $.getJSON('kf/pigai/select_zuzhi'),
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_num',
                            title: __('车牌号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_vin',
                            title: __('车架号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        // {field: 'ginseng_name', title: __('参统人'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {
                            field: 'learned_name',
                            title: __('被服务人'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'pigai_ctime',
                            title: __('批改日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'yw_join_time',
                            title: __('参统日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'pigai_type',
                            title: __('修改类型'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value == 1) {
                                    return '全单批改';
                                } else if (value == 4) {
                                    return '新车上牌';
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {
                                "1": __('初审中'),
                                "2": __('二级审核中'),
                                "3": __('财务审核中'),
                                "4": __('待修改'),
                                "5": __('审核通过'),
                                "6": __('市级审核中'),
                            },
                            custom: {
                                1: 'danger',
                                2: 'warning',
                                3: 'info',
                                4: 'default',
                                5: 'primary',
                                6: 'huang',
                                7: 'zi'
                            },
                            formatter: Table.api.formatter.label,
                            operate: false
                        },
                        {
                            field: 'lururen',
                            title: __('录入人'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'kf/pigai/details',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //权限判断
                                        // if (Config.rule.includes('237') || Config.rule.includes(237)) {
                                        //     //返回true时按钮显示,返回false隐藏
                                            return true;
                                        // }
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: __('编辑'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('编辑'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'kf/pigai/edit',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //权限判断
                                        console.log('Config.rule',Config.rule)
                                        if (Config.rule.includes('371') || Config.rule.includes(371)) {
                                            //返回true时按钮显示,返回false隐藏
                                            console.log('status',row.status)
                                            if (row.status == 0 || row.status == 4) {
                                                return true;
                                            }
                                        }
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: __('补录退费信息'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('补录退费信息'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'kf/pigai/bulu',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },{
                                    name: 'del',
                                    text: __('删除'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('删除'), // 提示
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: '', // 更换图标
                                    url: 'kf/pigai/del',
                                    confirm: '确认删除吗？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                    visible: function (row) {
                                        //权限判断
                                        if (Config.rule.includes('372') || Config.rule.includes(372)) {
                                            return true;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        allp: function () {
            $("#tui_time").on("dp.change", function () {
                var value = $('#tui_time').val()
                console.log('value', value)
                var etime = $("#endtime").html()

                var chaday = GetDateDiff(value, etime, "day")
                var zan_strattime = $("#zan_strattime").val()
                var zan_endtime = $("#zan_endtime").val()

                var shu = 0;
                if (zan_strattime != '' && zan_endtime != '') {
                    var shu = diy_time(zan_strattime, zan_endtime)
                    console.log(shu)
                }
                $("#tuiday").html(Number(chaday) - Number(shu))
                var jingfei = $("#jingfei").val()
                var tongchouday = $("#tongchouday").html()
                //一天的钱
                var oneday = (jingfei / tongchouday)
                console.log(oneday)
                var tuiday = $("#tuiday").html()
                var shitui = (Number(tuiday) * oneday)
                shitui = formatDecimal(shitui, 2)
                $("#tuifei").html(shitui)
            })
            Controller.api.bindevent();
        },
        details: function () {
            Controller.api.bindevent();
        },
        bulu: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    //开始时间选择后
    $("#c-plan_strattime").on("dp.change", function (e) {
        var time = $("#c-plan_strattime").val()
        gettimee(time)
        var end_time = $("#c-plan_endtime").val()
        var tianshu = diy_time(time, end_time)
        $("#datecount").html(tianshu)
    })
    //结束时间选择后
    $("#c-plan_endtime").on("dp.change", function () {
        var time = $("#c-plan_strattime").val()
        var end_time = $("#c-plan_endtime").val()
        var tianshu = diy_time(time, end_time)
        $("#datecount").html(tianshu)
    })

    function diy_time(time1, time2) {
        time1 = Date.parse(new Date(time1));
        time2 = Date.parse(new Date(time2));
        console.log('time1',time1)
        console.log('time2',time2)
        var num = (time2 - time1);
        if(num > 0){
            return time3 = Math.abs(parseInt( num / 1000 / 3600 / 24) + 1) + '天';
        }else{
            return time3 = '0天';
        }
    }

    function gettimee(date) {
        $.ajax({
            type: 'post',
            url: "/api/order/s_time", //发送路径
            data: {
                time: date
            }, //发送的数据
            //                    dataType: 'json',
            async: false, //是否异步
            success: function (data) {
                $("#c-plan_endtime").val(data.data)
            }
        })
    }

    return Controller;
});
